//
//  BZUCRegisterWrapperV.m
//  Pods
//
//  Created by quheng on 2017/8/29.
//
//

#import "BZUCRegisterWrapperV.h"
#import "BZUCRegisterV.h"
#import <Masonry/Masonry.h>
#import "BZUCInputVModel.h"
#import "BZUCInputV.h"
#import <BZPreHeaderComponent/BZPreHeaderComponent.h>


@interface BZUCRegisterWrapperV()

@property (nonatomic, weak) BZUCRegisterV *registerV;

@end

@implementation BZUCRegisterWrapperV





- (instancetype)init{
    
    self = [super init];
    
    if (self) {
        
        [self configureV];
        
    }
    
    return self;
}

- (void)configureV{
    
    //注册键盘出现的通知
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    //注册键盘消失的通知
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification object:nil];
    
    
    BZUCRegisterV *registerV = [[BZUCRegisterV alloc]init];
    [self addSubview:registerV];
    self.registerV = registerV;
    
    [registerV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
        make.size.equalTo(self);
    }];
}


- (void)setViewData:(NSArray *)viewData{
    
    _viewData = viewData;
    
    for (NSUInteger i =0; i<viewData.count; i++) {
        
        BZUCInputVModel *inputVModel = viewData[i];
        BZUCInputV *inputV = self.registerV.inputArr[i];
        inputV.inputVModel = inputVModel;

    }
    
}


///键盘显示事件
- (void) keyboardWillShow:(NSNotification *)notification {
    
    CGFloat kbHeight = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
    
    CGFloat offset = CGRectGetMaxY(self.registerV.registerBtn.frame) - (self.registerV.frame.size.height - kbHeight);
    if (offset > 0) {
        self.contentSize = CGSizeMake(0,self.self.frame.size.height +offset);
    }

    
    BOOL shouldOffset = NO;
    
    for (BZUCInputV *inputV in self.registerV.inputArr) {
        
        if (inputV.index <= 1) {
            
            if ([inputV.textF isFirstResponder]) {
                
                break;
            }
        }else{
        
            shouldOffset = YES;
        }
    }
    
    if (!shouldOffset) {
        return;
    }
    
    if (offset ) {
        
        self.contentOffset = CGPointMake(0, offset);
    }else{
        
        self.contentOffset = CGPointMake(0, 0);
        
    }
}

///键盘消失事件
- (void) keyboardWillHide:(NSNotification *)notify {
    
    self.contentOffset = CGPointMake(0,0);
    self.contentSize = CGSizeMake(SCREEN_WIDTH,SCREEN_HEIGHT-64);
}


- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    
}

@end
